package dart

class GameTests extends DartTestCase {

	void testHarshPenalty() {
		Game game = new Game(countDown: true, startingScore: 100, endingScore: 0,
				harshScale: 0.5, bounceBack: null, scoreLimit: null);
		PlayerGame playerGame = addPlayerGame(game);
		Round round = addRound(playerGame);

		assertEquals(5, game.harshPenalty(90))
		assertEquals(12.5, game.harshPenalty(75))
		assertEquals(25, game.harshPenalty(50))
		assertEquals(50, game.harshPenalty(0))
	}

	void testNextPlayer() {
		Game game = new Game(countDown: true, endingScore: 5,
				currentRound: 1, currentPlayer: 0)
		addPlayerGame(game);
		addPlayerGame(game);

		game.nextPlayer(10f);
		assertFalse(game.isWinningScore(10f))
		assertEquals(Game.IN_PROGRESS, game.status)
		assertEquals(1, game.currentRound)

		game.nextPlayer(10f);
		assertFalse(game.isWinningScore(10f))
		assertEquals(Game.IN_PROGRESS, game.status)
		assertEquals(2, game.currentRound)

		game.nextPlayer(0f);
		assertTrue(game.isWinningScore(0f))
		assertEquals(Game.FINISHING, game.status)
		assertEquals(2, game.currentRound)
	}

	void testEndGameWithOneWinner() {
		Game game = new Game(countDown: true, startingScore: 5,
				endingScore: 0, bounceBack: 0);

		PlayerGame winner = addPlayerGame(game);
		Round winnerRound = addRound(winner);
		addSuccess(winnerRound, 5, 1)

		PlayerGame loser = addPlayerGame(game);
		Round loserRound = addRound(loser);
		addPenalty(loserRound, 10, 10)

		game.endGame();
		assertEquals(Game.COMPLETED, game.status);
		assertEquals(PlayerGame.WON, winner.status);
		assertEquals(PlayerGame.LOST, loser.status);
	}

	void testEndGameWithTwoWinners() {
		Game game = new Game(countDown: true, startingScore: 5,
				endingScore: 0, bounceBack: 0);

		PlayerGame winner = addPlayerGame(game);
		Round winnerRound = addRound(winner);
		addSuccess(winnerRound, 5, 1)

		PlayerGame loser = addPlayerGame(game);
		Round loserRound = addRound(loser);
		addPenalty(loserRound, 10, 10)

		game.endGame();
		assertEquals(Game.COMPLETED, game.status);
		assertEquals(PlayerGame.WON, winner.status);
		assertEquals(PlayerGame.LOST, loser.status);
	}

	void testIsWinningScore() {
		Game game = new Game(countDown: true, endingScore: 0)
		assertFalse game.isWinningScore(1);
		assertTrue game.isWinningScore(0);
		assertTrue game.isWinningScore(-1);

		game.countDown = false;
		game.endingScore = 100;
		assertTrue game.isWinningScore(101);
		assertTrue game.isWinningScore(100);
		assertFalse game.isWinningScore(99);
	}

	void testIsBust() {
		Game game = new Game(countDown: true, endingScore: 0, bounceBack: 1)
		assertFalse game.isBust(-1)

		game.bounceBack = null;
		assertFalse game.isBust(0)
		assertFalse game.isBust(1)
		assertTrue game.isBust(-1)

		game.countDown = false;
		game.endingScore = 100;
		assertTrue game.isBust(101)
		assertFalse game.isBust(100)
		assertFalse game.isBust(99)
	}

	void testIsBeyondLimit() {
		Game game = new Game(countDown: true, scoreLimit: null)
		assertFalse game.isBeyondLimit(101);

		game.scoreLimit = 100;
		assertTrue game.isBeyondLimit(101);
		assertFalse game.isBeyondLimit(100);
		assertFalse game.isBeyondLimit(99);

		game.countDown = false;
		game.scoreLimit = 0;
		assertFalse game.isBeyondLimit(1);
		assertFalse game.isBeyondLimit(0);
		assertTrue game.isBeyondLimit(-1);
	}

	void testIsBeyondBounceBack() {
		Game game = new Game(countDown: true, bounceBack: null)
		assertFalse game.isBeyondBounceBack(0);

		game.bounceBack = 0;
		assertFalse game.isBeyondBounceBack(1);
		assertFalse game.isBeyondBounceBack(0);
		assertTrue game.isBeyondBounceBack(-1);

		game.countDown = false;
		game.bounceBack = 100;
		assertTrue game.isBeyondBounceBack(101);
		assertFalse game.isBeyondBounceBack(100);
		assertFalse game.isBeyondBounceBack(99);
	}
}